import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from "rxjs";

@Injectable({
  providedIn: 'root'
})
export class SellAddService {

  // 广告list--URL
  public advertisementListUrl = '/advert/list';

  // 文件上传
  public uploadFileUrl = '/common/upload/file';

  // 添加
  public addAdvertisementUrl = '/advert/save';

  // 修改 || 删除
  public updateUrl = '/advert/update';

  // 查询广告详情
  public advertisementDetailUrl = '/advert/details';

  constructor(public httpClient: HttpClient) { }

  /**
   * 查询广告tabledata
   * @param params 
   * @returns 
   */
  public advertisementTableData(params: any): Observable<any> {
    return this.httpClient.post(this.advertisementListUrl, params);
  }

  /**
   * 添加广告
   * @param params 
   * @returns 
   */
  public addAdvertisementData(params: any): Observable<any> {
    return this.httpClient.post(this.addAdvertisementUrl, params);
  }

  /**
   * 修改 || 删除广告
   * @param params 
   * @returns 
   */
  public editDeleteData(params: any): Observable<any> {
    return this.httpClient.post(this.updateUrl, params);
  }

  /**
   * 查询广告详情
   * @param params 
   * @returns 
   */
  public queryAdvertisementDetail(params: any): Observable<any> {
    return this.httpClient.post(this.advertisementDetailUrl, params);
  }

  /**
   * 文件上传
   * @param fileToUpload 
   * @returns 
   */
   public async uploadFileData(fileToUpload: File) {
    const formData: FormData = new FormData();
    formData.append('file', fileToUpload);
    formData.append('name', "adverts");
    return await this.httpClient.post(this.uploadFileUrl, formData).toPromise();
  }
}



